const CategoryModel = require('../model/CategoryModel')
const executeSql = require('../utils/query.js')
const { SUCCESS_RESPONSE, FAIL_RESPONSE } = require('../config/index.js')

// 获取所有分类
exports.getAllCategory = async (req, res) => {
  let category = await CategoryModel.getAllCategory()
  res.json(SUCCESS_RESPONSE(category))
}

// 测试搜索分类接口
exports.searchCategory = async (req, res) => {
  console.log('~~~~searchCategory~~~~')
  // console.log(req.query)
  let { category_name, is_show, start_date, end_date } = req.query
  let sql = `select * from category where 1 `
  //  根据查询条件，有传条件，动态的拼接sql语句
  if (category_name) {
    sql += ` and category_name like '%${category_name}%'`
  }
  if (is_show) {
    sql += ` and is_show = ${is_show}`
  }

  if (start_date && !end_date) {
    sql += ` and add_date > ${start_date}`
  }

  if (!start_date && end_date) {
    sql += ` and add_date < ${end_date}`
  }

  if (start_date && end_date) {
    sql += ` and add_date between ${start_date} and ${end_date}`
  }

  console.log('sql:', sql)
  let data = await executeSql(sql)
  res.json(data)
}

exports.CheckUpdCategoryName = async (req, res) => {
  let { category_id, category_name } = req.query
  const result = await CategoryModel.checkUpdCategoryName(
    category_id,
    category_name
  )
  if (result.length > 0) {
    // 有重名的
    res.json(FAIL_RESPONSE({}, '编辑分类名重复了', '00009'))
  } else {
    // 可用
    res.json(SUCCESS_RESPONSE({}, '编辑分类名可用'))
  }
}

exports.updCategory = async (req, res) => {
  console.log('~~~~~~~updCategory~~~~~~')
  console.log(req.body)

  // 2. 编写sql语句删除数据,
  let result = await CategoryModel.updCategory(req.body)

  if (result.affectedRows) {
    // 3. 成功
    res.json(SUCCESS_RESPONSE({}, '编辑分类成功'))
  } else {
    // 3. 失败
    res.json(FAIL_RESPONSE({}, '编辑分类失败', '00006'))
  }
}

exports.addCategory = async (req, res) => {
  console.log('~~~~~~~addCategory~~~~~~')
  // 1. 判断分类名称是否重名
  let cates = await CategoryModel.checkCategoryName(req.body.category_name)
  if (cates.length > 0) {
    // 匹配到了，说明重名
    // 3. 失败
    res.json(FAIL_RESPONSE({}, '分类名已存在，请更换一个', '00008'))
    return
  }
  // 2. 编写sql语句删除数据,
  let result = await CategoryModel.insertCategory(req.body)

  if (result.affectedRows) {
    // 3. 成功
    res.json(SUCCESS_RESPONSE({}, '添加分类成功'))
  } else {
    // 3. 失败
    res.json(FAIL_RESPONSE({}, '添加分类失败', '00006'))
  }
}

exports.delManyCategory = async (req, res) => {
  console.log('~~~~~~~delManyCategory~~~~~~')
  let { category_ids } = req.body
  // 1. 编写sql语句删除数据（model层）,
  await CategoryModel.deleteManyCategory(category_ids)
  // 3. 返回
  res.json(SUCCESS_RESPONSE({}, '删除所选分类成功'))
}

exports.delCategory = async (req, res) => {
  console.log('~~~~~~~delCategory~~~~~~')
  let { category_id } = req.body
  // 1. 编写sql语句删除数据（model层）,
  await CategoryModel.deleteCategory(category_id)
  // 3. 返回
  res.json(SUCCESS_RESPONSE({}, '删除分类成功'))
}

// 分页接口
exports.category = async (req, res) => {
  // 所有的查询字符串req.query接收的值都是字符串string类型
  let {
    page = 1,
    pagesize = 2,
    category_name,
    is_show,
    start_date,
    end_date,
  } = req.query
  // 1. 编写sql语句查询数据（model层）
  // 2. 查询数据
  let category = await CategoryModel.getCategory({
    page,
    pagesize,
    category_name,
    is_show,
    start_date,
    end_date,
  })
  // 查询符合条件的总记录数
  let count = await CategoryModel.getCategoryCount({
    category_name,
    is_show,
    start_date,
    end_date,
  })
  // 3. 成功返回数据
  res.json(SUCCESS_RESPONSE({ count, category }, '获取分类成功'))
}

exports.getOneCategory = async (req, res) => {
  let { category_id } = req.query
  // 1. 编写sql语句查询数据（model层）,// 2. 查询
  let category = await CategoryModel.find(category_id)
  res.json(SUCCESS_RESPONSE(category, '获取分类成功'))
}
